/**************************************************************************//**
  \file Peer2Peer.h

  \brief Peer-2-peer sample application header file.

  \author
    Atmel Corporation: http://www.atmel.com \n
    Support email: avr@atmel.com

  Copyright (c) 2008-2012, Atmel Corporation. All rights reserved.
  Licensed under Atmel's Limited License Agreement (BitCloudTM).

  \internal
    History:
    14.10.09 A. Taradov - Added FIFO for received packets
******************************************************************************/

#ifndef _PEER2PEER_H
#define _PEER2PEER_H

/******************************************************************************
                    Includes section
******************************************************************************/
#include "sliders.h"
#include "leds.h"
#include "buttons.h"

/******************************************************************************
                    Define(s) section
******************************************************************************/
#ifndef APP_USART_CHANNEL
  #define APP_USART_CHANNEL               USART_CHANNEL_1
#endif

#define APP_JOINING_INDICATION_PERIOD     500L // Period of blinking during starting network

#define APP_ENDPOINT                      1    // Endpoint will be useed
#define APP_PROFILE_ID                    1    // Profile Id will be used
#define APP_CLUSTER_ID                    1    // Cluster Id will be used

/*
  Parameters that affect data transfer:

  APP_TRANSMISSION_DELAY, ms
    Delay beetween transmitted packets.
    This delay may help in reducing amount of collisions thus increasing throughput.
    A value of 0 means no dealy is needed.

  APP_MAX_PACKET_SIZE, bytes
    Maximum amount of useful data that will be sent in one packet

  APP_DELAY_BEFORE_SEND, ms
    Delay beetween reception of first byte of current packet from UART and
    actual transmission of current packet over the air.
    Larger values of this parameter will result in higher throughput but
    also may lead to higher latencies.
    A value of 0 means no dealy is needed.
*/

#define APP_TRANSMISSION_DELAY            20

#if APP_FRAGMENTATION
  #define  APP_MAX_PACKET_SIZE            150
  // Relatively large value is used here to let application receive enough data to
  // make fragmentation feature work.
  #define  APP_DELAY_BEFORE_SEND          3000
#else // No fragmentation is alowed
  #define  APP_MAX_PACKET_SIZE            60
  #define  APP_DELAY_BEFORE_SEND          0
#endif // APP_FRAGMENTATION


// Size of APS payload (user data + message ID)
#define APP_APS_PAYLOAD_SIZE              (APP_MAX_PACKET_SIZE + 1)

// Size of temporary buffer (FIFO) to store data received from air.
// This size should be larger than APP_MAX_PACKET_SIZE.
#define APP_DATA_IND_BUFFER_SIZE          APP_APS_PAYLOAD_SIZE

// Receive buffer size for USART.
#define APP_USART_RX_BUFFER_SIZE          100

// Transmit buffer size for USART.
#define APP_USART_TX_BUFFER_SIZE          100

#if APP_DETECT_LINK_FAILURE == 1
// Maximum amount of retries before rejoin is attempted
#define MAX_RETRIES_BEFORE_REJOIN         3
#endif // APP_DETECT_LINK_FAILURE

// Some sanity checks
#if APP_DATA_IND_BUFFER_SIZE < APP_MAX_PACKET_SIZE
  #error APP_DATA_IND_BUFFER_SIZE must be larger or equal to APP_MAX_PACKET_SIZE
#endif

#if APP_FRAGMENTATION
  #ifndef _APS_FRAGMENTATION_
    #error BitCloud must be built with fragmentation feature enabled
  #endif

  #if APP_APS_PAYLOAD_SIZE > (CS_APS_MAX_BLOCKS_AMOUNT * APS_MAX_ASDU_SIZE)
    #error APP_MAX_PACKET_SIZE  must be less or equal to (CS_APS_MAX_BLOCKS_AMOUNT * APS_MAX_ASDU_SIZE)
  #endif
#else
  #if APP_APS_PAYLOAD_SIZE > APS_MAX_ASDU_SIZE
    #error APP_APS_PAYLOAD_SIZE must be less or equal to APS_MAX_ASDU_SIZE
  #endif
#endif

// Common application state definition
typedef enum
{
  APP_INITIAL_STATE,                           // Initial state
  APP_NETWORK_JOINING_STATE,                   // Attempting join the network
  APP_NETWORK_JOINED_STATE,                    // Successfully joined
  APP_NETWORK_LEAVING_STATE,                   // Leaving from the network
  APP_ERROR_STATE                              // Error state (runtime error occured)
} AppState_t;

// Network data transmission state
typedef enum
{
  APP_DATA_TRANSMISSION_SENDING_STATE,         // APS Data Request was not sent yet
  APP_DATA_TRANSMISSION_BUSY_STATE,            // APS Data Request was sent (confirm waiting)
  APP_DATA_TRANSMISSION_WAIT_STATE,            // Waiting a data block from USART
  APP_DATA_TRANSMISSION_READY_STATE,           // Ready to send new APS Data Request
  APP_DATA_TRANSMISSION_STOP_STATE             // Inter-frame delay
} AppDataTransmissionState_t;

BEGIN_PACK
// Application network messsage descriptor
typedef struct PACK
{
  uint8_t messageId;                           // message ID
  uint8_t data[APP_MAX_PACKET_SIZE];           // data
} PACK AppMessage_t;

// Application network messsage buffer descriptor
typedef struct PACK
{
  uint8_t header[APS_ASDU_OFFSET];                    // Auxiliary header (required by stack)
  AppMessage_t message;                               // Application message
  uint8_t footer[APS_AFFIX_LENGTH - APS_ASDU_OFFSET]; // Auxiliary footer (required by stack)
} PACK AppMessageBuffer_t;
END_PACK


// Leds aliases definition hardware platfor supported if
#define APP_NETWORK_STATUS_LED            LED_GREEN   // Network status LED
#define APP_RECEIVING_STATUS_LED          LED_YELLOW  // Data receiving status LED
#define APP_SENDING_STATUS_LED            LED_RED     // Data transmission status LED

#endif // ifndef _PEER2PEER_H

// eof peer2peer.h
